package com.xbq.exceleditor.ui

import android.text.SpannableString
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.widget.TextView
import com.xbq.exceleditor.R
import com.xbq.xbqsdk.XbqSdk

/**
 * Author: liaohaiping
 * Time: 2020-10-30
 * Description: 初始化注册登录界面的已阅读并同意隐私协议复选框
 */
class CkbReadPrivacySetup(val ckb:TextView) {
    fun setup() {
        val context = ckb.context
        val text = context.getText(R.string.ckb_read_agree_privacy_agreement)
        val startOfUserAgreement = text.indexOf("《用户协议》")
        val endOfUserAgreement = startOfUserAgreement + "《用户协议》".length
        val userAgreementClickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                XbqSdk.startUserAgreement(context)
            }
        }
        val startOfPrivacy = text.indexOf("《隐私政策》")
        val endOfPrivacy = startOfPrivacy + "《隐私政策》".length
        val privacyClickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                XbqSdk.startPrivacy(context)
            }

        }

        SpannableString(text).apply {
            setSpan(
                userAgreementClickableSpan, startOfUserAgreement, endOfUserAgreement,
                SpannableString.SPAN_INCLUSIVE_INCLUSIVE
            )
            setSpan(
                privacyClickableSpan,
                startOfPrivacy,
                endOfPrivacy,
                SpannableString.SPAN_INCLUSIVE_INCLUSIVE
            )
            ckb.setText(this)
            ckb.movementMethod = LinkMovementMethod.getInstance()
        }
    }
}